package com.hsnn.medstgmini.base.dict.dao.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hsnn.medstgmini.base.dict.dao.DicPackunitLogDao;
import com.hsnn.medstgmini.dao.impl.HibernateSqlUtilImpl;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 包装单位字典维护记录Dao
 * @author 王炳
 * @date 2015年7月27日
 */
@Repository
public class DicPackunitLogDaoImpl implements DicPackunitLogDao{
	
	@Autowired
	private HibernateSqlUtilImpl dao;
	
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	/**
	 * 
	 *@category 获取包装单位字典维护记录列表
	 *@author wangbing
	 *@date 2015年7月27日
	 *String
	 */
	@Override
	public Pagination  getDicPackunitLogList(Pagination page){
		Session session = sessionFactory.getCurrentSession();
		String startTime = page.getConditions().get("startTime");
		String endTime = page.getConditions().get("endTime");
		String packunitId = page.getConditions().get("packunitId");
		String packunitCode =page.getConditions().get("packunitCode");
		StringBuffer sql1 = new StringBuffer();
		sql1.append("	 SELECT sql_calc_found_rows a.packunit_code packunitCode,a.packunit_name packunitName,a.status status,  ")
			.append("	 s.name updUser,a.upd_datetime updDatetime,a.log_id logId,a.detail FROM  dic_packunit_log a  ")
			.append("	 LEFT JOIN sys_user s ON s.id=a.upd_user   ")
			.append("    WHERE a.packunit_id=:packunitId     ");
		StringBuffer sql2 = new StringBuffer();
		sql2.append("   SELECT sql_calc_found_rows a.packunit_code packunitCode,a.packunit_name packunitName,   ")
		    .append(" 	b.status status,s.name updUser,b.upd_datetime updDatetime,b.log_id logId,b.detail  FROM  dic_packunit_log b  ")
		    .append(" 	INNER JOIN dic_packunit a ON a.packunit_id=b.packunit_id  ")
		    .append(" 	LEFT JOIN sys_user s ON s.id=b.upd_user  ")
		    .append(" 	WHERE b.packunit_id=:packunitId  ");
		/*String packunitId = page.getConditions().get("packunitId");
		String startDate = page.getConditions().get("startDate");
		String endDate = page.getConditions().get("endDate");
		StringBuffer  sql = new StringBuffer();
		sql.append(" select a.log_id logId,a.packunit_code packunitCode,a.packunit_name packunitName,  ")
		   .append(" a.status status,date_format(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,b.name updUser,a.detail detail ")
		   .append(" from dic_packunit_log a left join sys_user b on b.id=a.upd_user where a.packunit_id = :packunitId ");
		Map<String,String> params = new HashMap<String, String>();
		params.put("packunitId", packunitId);*/
		if(StringUtils.isNotBlank(packunitCode)){
			if(StringUtils.isNotBlank(startTime)){
				sql2.append(" and b.upd_datetime >= :startTime ");
			}
			if(StringUtils.isNotBlank(endTime)){
				sql2.append(" and b.upd_datetime <= :endTime ");
			}
			sql2.append(" order by b.upd_datetime desc");
			Query query = session.createSQLQuery(sql2.toString());
			Query queryCount = session.createSQLQuery("select found_rows() ");
			query.setString("packunitId", packunitId);
			if(StringUtils.isNotBlank(startTime)){
				query.setString("startTime", startTime+" 00:00:00");
			}
			if(StringUtils.isNotBlank(endTime)){
				query.setString("endTime", endTime+" 23:59:59");
			}
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getMaxResults());
			query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			page.setRows(query.list());
			page.setRecords(Long.parseLong(queryCount.uniqueResult().toString()));
			return page;
		}else{
			if(StringUtils.isNotBlank(startTime)){
				sql1.append(" and a.upd_datetime >= :startTime ");
			}
			if(StringUtils.isNotBlank(endTime)){
				sql1.append(" and a.upd_datetime <= :endTime ");
			}
			sql1.append(" order by a.upd_datetime desc");
			Query query = session.createSQLQuery(sql1.toString());
			Query queryCount = session.createSQLQuery("select found_rows() ");
			query.setString("packunitId", packunitId);
			if(StringUtils.isNotBlank(startTime)){
				query.setString("startTime", startTime+" 00:00:00");
			}
			if(StringUtils.isNotBlank(endTime)){
				query.setString("endTime", endTime+" 23:59:59");
			}
			query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getMaxResults());
			page.setRows(query.list());
			page.setRecords(Long.parseLong(queryCount.uniqueResult().toString()));
			return page;
		}
	}
	
	/**
	 * 
	 *@category 获取包装单位字典维护记录详情
	 *@author wangbing
	 *@date 2015年7月27日
	 *String
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>>  getDicPackunitLogDetail(Pagination page){
		Session session = sessionFactory.getCurrentSession();
		String logId = page.getConditions().get("logId");
		StringBuffer  sql = new StringBuffer();
		sql.append(" select a.packunit_id packunitId,a.packunit_code packunitCode,a.packunit_name packunitName,  ")
		   .append(" a.status status,date_format(a.upd_datetime,'%Y-%m-%d %H:%i:%S') updDatetime,b.name updUser ")
		   .append(" from dic_packunit_log a left join sys_user b on b.id=a.upd_user where a.packunit_id =  ")
		   .append(" (select packunit_id from dic_packunit_log where log_id = :logId)  ")
		   .append(" and a.upd_datetime <= (select upd_datetime from dic_packunit_log where log_id = :logId) ")
		   .append(" order by a.upd_datetime desc limit 2");
		Query query = session.createSQLQuery(sql.toString());
		query.setString("logId", logId);
		query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		return query.list();
	}
}
